﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
using System.Timers;
using Wicresoft.TFSAutoNotify.Common;

namespace Email.WindowsService
{
    public partial class Email : ServiceBase
    {
        Tfs_AutoNotifyEntities TfsDB = new Tfs_AutoNotifyEntities();
        System.Timers.Timer timesDo = new System.Timers.Timer();
        public Email()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {

            timesDo.Elapsed += new System.Timers.ElapsedEventHandler(CheckShelve);
            timesDo.AutoReset = true;
            timesDo.Enabled = true;
            timesDo.Interval = 1000;
            timesDo.Start();
            GC.KeepAlive(timesDo);
        }

        protected override void OnStop()
        {
        }
        public void CheckShelve(object source, ElapsedEventArgs e)
        {

            var noSent = TfsDB.ShelveInfos.Where(p => p.IsSent == false); 

            foreach (var x in noSent)
            {
                    SendEmail(x);
                    x.IsSent = true;
                    x.SentTime = DateTime.Now;
                    TfsDB.SaveChanges();
            }

        }
        public void SendEmail(ShelveInfo _shelveInfo)
        {

            string to = _shelveInfo.MailTo;
            string from = _shelveInfo.MailFrom;
            string subject = _shelveInfo.Subject;
            string body = _shelveInfo.Body;
            MailMessage message = new MailMessage(from, to, subject, body);
            char[] _char = new char[10];
            _char[0] = ';';
            MailAddressCollection mailCCAdress = new MailAddressCollection();
            foreach (var x in SeparateStr(_shelveInfo.CC, _char))
            {
                if (x != null)
                {
                    message.CC.Add(x);
                }
            }
            foreach (var x in SeparateStr(_shelveInfo.BCC, _char))
            {
                if (x != null && x.Contains("@"))
                {
                    message.Bcc.Add(x);
                }
            }
            SmtpClient client = new SmtpClient("172.24.146.133", 25);
            // Credentials are necessary if the server requires the client 
            // to authenticate before it will send e-mail on the client's behalf.
            //client.Credentials = CredentialCache.DefaultNetworkCredentials;

            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage1(): {0}",
                      ex.ToString());
            }
        }
        public static List<string> SeparateStr(string str, char[] sepChar)
        {
            List<string> strList = new List<string>();

            string[] _x = str.Split(sepChar);

            foreach (var _y in _x)
            {
                if (_y != null)
                {
                    strList.Add(_y);
                }
            }

            return strList;
        }
    }
}
